using System.Linq;
using ArticleManagementSystem.Api.Entity;
using ArticleManagementSystem.Api.Params;
using ArticleManagementSystem.Api.Repostisory;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ArticleManagementSystem.Api.Controllers
{
    //获取token权限
    // [Authorize]

    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        private readonly IRepostisory<Users> _usersRepostisory;
        private readonly IRepostisory<Article> _articleRepostisory;

        public UsersController(IRepostisory<Users> userRepostisoy)
        {
            _usersRepostisory = userRepostisoy;
        }

        // 查询用户列表
        public dynamic Get()
        {
            var users = _usersRepostisory.Table;
            return new
            {
                Code = 200,
                Data = users,
                Msg = "获取用户信息成功"
            };
        }


        // 注册
        [AllowAnonymous]
        [HttpPost, Route("registry")]
        public dynamic Post(CreateUsers newUser)
        {
            var Username = newUser.Username.Trim();
            var Password = newUser.Password.Trim();
            var RePassword = newUser.RePassword.Trim();


            if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
            {

                if (!Password.Equals(RePassword))
                {
                    return new
                    {
                        code = 200,
                        data = "",
                        msg = "两次输入的密码不一致"
                    };
                }

                var users = _usersRepostisory.Table;
                var isHaveUser = users.Where(x => x.Username.Equals(Username)).ToList();

                if (isHaveUser.Count() == 0)
                {
                    var addUser = new Users
                    {
                        Password = Password,
                        Username = Username
                    };

                    _usersRepostisory.Insert(addUser);

                    return new
                    {
                        code = 200,
                        data = addUser,
                        msg = "用户创建成功"
                    };
                }
                else
                {
                    //该用户已存在
                    return new
                    {
                        code = 104,
                        data = "",
                        msg = "该用户已存在"
                    };
                }

            }
            else
            {
                //用户名密码不能为空
                return new
                {
                    code = 104,
                    data = "",
                    msg = "用户名密码不能为空"
                };
            }
        }


        // 登录
        [AllowAnonymous]
        [HttpPost, Route("login")]
        public dynamic post(Login login)
        {
            var Username = login.Username.Trim();
            var Password = login.Password.Trim();
            if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
            {
                var user = _usersRepostisory.Table;
                var u = user.Where(x => x.Username.Equals(Username) && x.Password.Equals(Password));
                if (u.Count() != 0)
                {
                    return new
                    {
                        code = 200,
                        data = u,
                        msg = "登陆成功"
                    };
                }
                else
                {
                    return new
                    {
                        code = 104,
                        data = u,
                        msg = "用户名密码错误"
                    };
                }
            }
            else
            {
                return new
                {
                    code = 104,
                    msg = "请输入用户名密码"
                };
            }
        }


        //查询
        [HttpPost, Route("query")]
        public dynamic GetQuery(Myquery querys)
        {
            var querydata = querys.query;
            var pageIndex = querys.PageIndex;
            var pageSize = querys.PageSize;
            var articletype = querys.ArticleType;
            var users = _articleRepostisory.Table;
            dynamic resdata;

            if (!string.IsNullOrEmpty(articletype))
            {
                resdata = users.Where(x => x.Title.Contains(querydata) 
                || x.Remarks.Contains(querydata))
                .ToList();
            }
            else
            {
                resdata = users.Where(x => x.Title.Contains(querydata) 
                || x.Remarks.Contains(querydata) 
                || x.ArticleType.Contains(articletype))
                .ToList();
            }

            var u = resdata.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var res = new
            {
                StatusCode = 1000,
                Data = new { Data = u, Pager = new { pageIndex, pageSize, rowsTotal = resdata.Count() } },
                Msg = "查询成功"
            };
            return res;
        }



    }



}